<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Role_Functions
 *
 * @author Samuel Pulta
 */
class Role_Functions extends Functions {
    
    public function load_table_brand_category($db){
        $datas = $db->query("SELECT *
                            ,
                            d.id as dDepID, d.is_active as dIs_active,
                            r.id as rRoleID, bc.id as bcBrandID, bc.is_active as bcIs_active
                            FROM " . $db->tbl_name(TB_BRAND_CATEGORY) . " bc
                            LEFT JOIN " . $db->tbl_name(TB_DEPARTMENT) . " d ON bc.department_id=d.id
                            LEFT JOIN " . $db->tbl_name(TB_ROLES) . " r ON bc.role_id=r.id
                            
                            WHERE bc.is_active={$db->escape(1)}", "array");
        $ret = "";
        $count = 1;
        foreach($datas as $data){
            $json_data = json_encode($data);
            $ret .= '<tr>';
                $ret .= '<td>' . $count++ . '</td>';
                $ret .= '<td>' . $data['role_name'] . '</td>';
                $ret .= '<td>' . $data['department_name'] . '</td>';
                $ret .= '<td>' . $data['brand_category'] . '</td>';
                $ret .= '<td data-json="' . $json_data . '" class="TAC">';
                    $ret .= '<a href="#"><span class="icon-pencil tip" data-original-title="Edit ' . $data['brand_category'] . '"></span></a>';
                    $ret .= '<a href="#"><span class="icon-trash tip" data-original-title="Edit ' . $data['brand_category'] . '"></span></a>';
                $ret .= '</td>';
            $ret .= '</tr>';
        }
        
        return $ret;
    }
    
    public function load_table_department($db){
        $datas = $db->query("SELECT *,
                            d.id as dDepID, d.is_active as dIs_active,
                            r.id as rRoleID
                            FROM " . $db->tbl_name(TB_DEPARTMENT) . " d
                            LEFT JOIN " . $db->tbl_name(TB_ROLES) . " r ON d.role_id=r.id
                            WHERE d.is_active={$db->escape(1)}", "array");
        $ret = "";
        $count = 1;
        foreach($datas as $data){
            $json_data = json_encode($data);
            $ret .= '<tr>';
                $ret .= '<td>' . $count++ . '</td>';
                $ret .= '<td>' . $data['department_name'] . '</td>';
                $ret .= '<td>' . $data['role_name'] . '</td>';
                $ret .= '<td data-json="' . $json_data . '" class="TAC">';
                    $ret .= '<a href="#"><span class="icon-pencil tip" data-original-title="Edit ' . $data['department_name'] . '"></span></a>';
                    $ret .= '<a href="#"><span class="icon-trash tip" data-original-title="Edit ' . $data['department_name'] . '"></span></a>';
                $ret .= '</td>';
            $ret .= '</tr>';
        }
        
        return $ret;
    }
    
    public function load_table_roles($db){
        $datas = $db->query("SELECT *, id as roleID FROM " . $db->tbl_name(TB_ROLES) . " WHERE is_active={$db->escape(1)}", "array");
        $ret = "";
        $count = 1;
        foreach($datas as $data){
            $json_data = json_encode($data);
            $ret .= '<tr>';
                $ret .= '<td>' . $count++ . '</td>';
                $ret .= '<td>' . $data['role_name'] . '</td>';
                $ret .= "<td data-json='" . $json_data . "' class='TAC'>";
                    $ret .= '<a data-action="role" data-type="edit" class="role_configuration_actions"  href="#"><span class="icon-pencil tip" data-original-title="Edit ' . $data['role_name'] . '"></span></a>';
                    $ret .= '<a data-action="role" data-type="remove" class="role_configuration_actions"  href="#"><span class="icon-trash tip" data-original-title="Remove ' . $data['role_name'] . '"></span></a>';
                $ret .= '</td>';
            $ret .= '</tr>';
        }
        
        return $ret;
    }
    
    public function load_roles($db,$value){
        $roles = $db->query("SELECT * FROM " . $db->tbl_name(TB_ROLES) . " WHERE is_active={$db->escape(1)}", "array");
        $ret = "";
        $ret .= '<option value="">Choose Role</option>';
        if(!empty($value)){
            foreach($roles as $role){
                if($value == $role['id']){
                    $selected = "selected=selected";
                }else{
                    $selected = "";
                }
                $ret .= '<option value="' . $role["id"] . '" ' . $selected . ' >' . $role["role_name"] . '</option>';
            }
        }
        return $ret;
    }
    
    
    public function load_department($db,$value){
        $roles = $db->query("SELECT * FROM " . $db->tbl_name(TB_DEPARTMENT) . " WHERE is_active={$db->escape(1)}", "array");
        $ret = "";
        $ret .= '<option value="">Choose Department</option>';
        if(!empty($value)){
            foreach($roles as $role){
                if($value == $role['id']){
                    $selected = "selected=selected";
                }else{
                    $selected = "";
                }
                $ret .= '<option value="' . $role["id"] . '" ' . $selected . ' >' . $role["department_name"] . '</option>';
            }
        }
        
        
        return $ret;
    }
    
    public function load_brand_category($db,$value){
        $roles = $db->query("SELECT * FROM " . $db->tbl_name(TB_BRAND_CATEGORY) . " WHERE is_active={$db->escape(1)}", "array");
        $ret = "";
        $ret .= '<option value="">Choose Brand Category</option>';
        if(!empty($value)){
            foreach($roles as $role){
                if($value == $role['id']){
                    $selected = "selected=selected";
                }else{
                    $selected = "";
                }
                $ret .= '<option value="' . $role["id"] . '" ' . $selected . ' >' . $role["brand_category"] . '</option>';
            }
        }
        
        return $ret;
    }
}

?>
